//
//  ICMBottomBarView.m
//  ICrEAM
//
//  Created by rainimute on 16/11/1.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMBottomBarView.h"

@interface ICMBottomBarView ()


@end

@implementation ICMBottomBarView

- (instancetype)initBottomBarViewWithTitles:(NSArray *)titles normalImages:(NSArray *)normalImages seletedImages:(NSArray *)seletedImages andFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.bottomButtons = [NSMutableArray array];
        for (NSInteger i=0; i<titles.count; i++) {
            UIButton *bottomButton = [[UIButton alloc]initWithFrame:CGRectMake(i*frame.size.width/titles.count, 0, frame.size.width/titles.count, 70)];
            bottomButton.backgroundColor = TitleColor;
            //
            UIView *imageAndTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 25, 20, 20)];
            imageAndTitleView.backgroundColor = [UIColor clearColor];
            imageAndTitleView.userInteractionEnabled = NO;
            //
            UIImageView *buttonImageView = [[UIImageView alloc]initWithFrame:CGRectMake(2, 0, 20, 20)];
            buttonImageView.image = normalImages[i];
            [imageAndTitleView addSubview:buttonImageView];
            //
            CGSize titleSize = [titles[i] sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20]}];
            UILabel *buttonLabel = [[UILabel alloc]initWithFrame:CGRectMake(24, 0, titleSize.width+2, 20)];
            [buttonLabel setUpLabelWithText:titles[i] alignment:NSTextAlignmentLeft textSize:20 textColor:[UIColor whiteColor]];
            [imageAndTitleView addSubview:buttonLabel];
            if ([titles[i] isEqualToString:@"录 音"]) {
                self.recorderLabel = buttonLabel;
            }
            
            
            float width = titleSize.width+26;//2+20+2+titleSize.width+2;
            imageAndTitleView.frame = CGRectMake((CGRectGetWidth(bottomButton.frame)-width)/2, 25, width, 20);
            
            [bottomButton addSubview:imageAndTitleView];
            [bottomButton setUpButtonWithNormolTitle:nil selectedTitle:nil normolTitleSize:0 selectedTitleSize:0 normolTitleColor:nil selectedTitleColor:nil normolBcakImage:nil selectedBackImage:nil actionSEL:@selector(bottomButtonAction:) target:self];
            //
            if (i<titles.count-1) {
                UIView *sepView = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetWidth(bottomButton.frame)-1, 0, 1, CGRectGetHeight(bottomButton.frame))];
                sepView.backgroundColor = [UIColor whiteColor];
                [bottomButton addSubview:sepView];
            }
            [self addSubview:bottomButton];
            [self.bottomButtons PACA_AddObject:bottomButton];
        }
    }
    return self;
}

- (void)bottomButtonAction:(UIButton*)sender{
    if ([self.delegate respondsToSelector:@selector(bottomBarViewDidSelectIndex:)]) {
        NSInteger index = [self.bottomButtons indexOfObject:sender];
        [self.delegate bottomBarViewDidSelectIndex:index];
    }
}

- (void)setButtonEnable:(NSInteger)index{
    if (self.bottomButtons.count<=index) {
        return;
    }
    UIButton *button = self.bottomButtons[index];
    button.alpha = 1.0;
    button.enabled = YES;
}
- (void)setButtonDisEnable:(NSInteger)index{
    if (self.bottomButtons.count<=index) {
        return;
    }
    UIButton *button = self.bottomButtons[index];
    button.alpha = 0.5;
    button.enabled = NO;
}

+ (instancetype)createBottomBarViewWithTitles:(NSArray *)titles normalImages:(NSArray *)normalImages seletedImages:(NSArray *)seletedImages andFrame:(CGRect)frame{
    return [[ICMBottomBarView alloc]initBottomBarViewWithTitles:titles normalImages:normalImages seletedImages:seletedImages andFrame:frame];
}

@end
